package com.panda.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panda.entity.po.History;
import com.panda.mapper.HistoryMapper;
import com.panda.service.HistoryService;
import com.panda.utils.RedisCache;
import com.panda.utils.RespondResult;
import com.panda.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

/**
 * (History)表服务实现类
 *
 * @author makejava
 * @since 2022-05-16 19:53:54
 */
@Service("historyService")
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {

    @Autowired
    private RedisCache redisCache;

    /**
     * 从redis获取所有历史记录
     *
     * @return
     */
    @Override
    public RespondResult getAllHistory(int pageNum, int pageSize) {

        ArrayList<String> watchHistories = new ArrayList<>();

        //获取当前登陆用户
        Long userId = SecurityUtils.getUserId();

        //redis key
        String key = "user:history:" + userId;
        //从redis获取历史记录
        Set<ZSetOperations.TypedTuple<String>> scoreWithScores =
                redisCache.zReverseRangeWithScores(key, pageNum, pageSize);
        //创建迭代器
        Iterator<ZSetOperations.TypedTuple<String>> iterator = scoreWithScores.iterator();
        BigDecimal bigDecimal = null;
        while (iterator.hasNext()) {
            ZSetOperations.TypedTuple<String> next = iterator.next();
            if (next.getScore() != null) {
                bigDecimal = BigDecimal.valueOf(next.getScore());
            }
            assert bigDecimal != null;
            System.out.println("==》ID： " + next.getValue() + " 时间： " + bigDecimal.toPlainString());
            if (next.getValue() != null) {
                watchHistories.add(next.getValue());
            }
        }
        // 这里返回List给到前端
        return RespondResult.okResult(watchHistories);

    }

    @Override
    public RespondResult updateHistory(String key, String value) {
        //阈值-历史最多10个
        long top = 10;
        // 拿到存入Redis里数据的唯一分值
        Double score = redisCache.zScore("user:history:" + key, value);
        //检索是否有旧记录  1.无则插入记录值  2.有则删除 再次插入
        if (null != score) {
            //删除旧的
            redisCache.zRemove(key, value);
        }
        //加入新的记录，设置当前时间戳为分数score
        redisCache.zAdd(key, value, System.currentTimeMillis());
        //获取总记录数
        Long aLong = redisCache.zZCard(key);
        if (aLong > top) {
            //获取阈值200之后的记录  (0,1] 并移除
            redisCache.zRemoveRange(key, 0, aLong - top - 1);
        }
        return RespondResult.okResult();
    }

    @Override
    public RespondResult deleteAllHistory() {
        return null;
    }

    @Override
    public RespondResult deleteHistory(String key) {
        return null;
    }





}

